$(function () {
    // 渲染页面
    init()
    //渲染top10
    topinit()
    //公告渲染
    noticeinit()
    moshui()
    // $(document).on('click','.nologin',function () {
    //     window.location.href='/unAuthorize/login'
    // })
    $(document).on('click','.likeicon',function () {
        var li = $(this).closest('li')
        var that = $(this)
        var div = $(this).closest('div')
        var userid = li.find('.userid').text()
        var aid = li.find('.articleid').text()
        if($('.fa-th-large').text()=='登录/注册')
        {
            window.location.href='/unAuthorize/login'
            return
        }
        // var param = {id:userid,aid:aid}
        console.log(userid)
        console.log(aid)
        $.ajax({
            url:'/article/liking?id='+aid+'&aid='+userid,
            type: 'GET',
            // data:param,
            dataType: "json",
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:function (res) {
                console.log(res)
                if (res.code==200){
                    $.toast({
                        title: '通知',
                        subtitle: '1秒前',
                        content: '点赞成功',
                        type: 'success',
                        delay: 3000,
                        pause_on_hover: false
                    });
                    console.log()
                    var suplike = div.find('.suplike').text()
                    suplike = Number(suplike)+1
                    console.log(suplike)
                    div.find('.suplike').text(suplike)


                    that.addClass('active')
                }
                else {
                    $.toast({
                        title: '通知',
                        subtitle: '1秒前',
                        content: res.msg,
                        type: 'error',
                        delay: 3000,
                        pause_on_hover: false
                    });
                    return;
                }
            }

        })
    })


    function init() {

        $.ajax({
            url: '/unAuthorize/getArticleList',
            type: 'Get',
            dataType: "json",
            // data: ,
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    if (res.code!==200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'success',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                    var data  = res.data
                    console.log(data)
                    xianshi(10,1,data)
                    var page = Math.ceil(data.length/10)
                    $('#pagination').pagination({
                        pageCount: page,
                        jump: true,
                        coping: true,
                        homePage: '首页',
                        endPage: '末页',
                        prevContent: '上页',
                        nextContent: '下页',
                        current:1,  //当前第几页
                        callback:function (api) {
                            console.log(api.getCurrent())
                            xianshi(10,api.getCurrent(),data)
                        }
                    })
                }
        })

    }



    function xianshi(num,page,data) {
        $('.listui').children().remove()
        var page2 = (page-1)*10
        for (let i = 0; i < num; i++) {
            if(data[i+page2]){
                var $str = $('<li><div class="userid" style="display: none">'+data[i+page2].user.id+'</div><div class="articleid" style="display: none">'+data[i+page2].article.id+'</div><div class="card-contentli mt-1">' +
                    '<div class="card-content-left-title">' +
                    '<a href="/unAuthorize/article?id='+data[i+page2].article.id+'" title="点击查看文章详情"><span>'+data[i+page2].article.title+'</span></a>' +
                    '</div>' +
                    '<div class="card-content-left-article">'+data[i+page2].article.content.replace(/<[^>]+>/g,"")+'</div>' +
                    '<div class="card-content-left-detail"><div class="card-content-left-detail-img" ><a href="/unAuthorize/blogger?id='+data[i+page2].user.id+'" title="点击查看博主"><img src="'+data[i+page2].user.img+'" class="img-circle elevation-1 img-fluid" alt="获取失败"></a>' +
                    ' <span>'+data[i+page2].user.username+'</span></div>' +
                    '<div class="card-content-left-detail-time"><i class="fa-thumbs-up likeicon fa" title="点赞" aria-hidden="true"></i><span class="suplike"> '+data[i+page2].article.likeNumber+' </span><span>|</span><a href="/unAuthorize/article?id='+data[i+page2].article.id+'"><i class="fa-eye fa" title="浏览量"></i></a><span>'+data[i+page2].article.clickNumber+' |</span><a href="/unAuthorize/article?id='+data[i+page2].article.id+'"><i class="fa fa-comment" title="评论"></i></a><span>'+data[i+page2].article.commentNumber+'</span></div>' +
                    '</div>'+
                    '</div></li>')
                $('.listui').append($str)
            }
        }
    }

    function topinit() {
        $.ajax({
            url: '/unAuthorize/getTop10',
            type: 'Get',
            dataType: "json",
            // data: ,
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    if (res.code!==200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'success',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                    var data  = res.data
                    console.log(data)
                    top10(data)
                }
        })
    }

    function top10(data) {
        $('.topli').children().remove()

        for (let i = 0; i < 10; i++) {
            if(data[i]){
                var index = i+1
                var $str = $(
                    '<a class="content-a" href="/unAuthorize/blogger?id='+data[i].user.id+'"><div class="card-content-right-rank mt-3 mr-1">\n' +
                    '<div class="index">'+index+'</div>'+
                    '<div class="col-md-3"><img src="'+data[i].user.img+'" alt="图片丢失"></div>' +
                    '<div class="col-md-7 topnameclick"><span class="title ">'+data[i].user.username+'</span>' +
                    '<span class="title ">点击量:'+data[i].clickNumber+'</span>'+
                    '</div>'+
                    '</div></a>')
                $('.topli').append($str)
            }
        }


    }

    function noticeinit() {
        $.ajax({
            url: '/unAuthorize/getNotice',
            type: 'Get',
            dataType: "json",
            // data: ,
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    if (res.code!==200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'success',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                    var data  = res.data
                    console.log(data)
                    $('.notictitle').text(data.title)
                    $('.notice-content').text(data.content)
                    $('.notice-name').text(data.name)
                    $('.notice-time').text(data.updateTime)




                }
        })


    }




})
function moshui() {
    var canvas = document.getElementById("c");
    var ctx = canvas.getContext("2d");
    var c = $("#c");
    var w, h;
    var pi = Math.PI;
    var all_attribute = {
        num: 100,            			 // 个数
        start_probability: 0.1,		     // 如果数量小于num，有这些几率添加一个新的
        radius_min: 1,   			     // 初始半径最小值
        radius_max: 2,   			     // 初始半径最大值
        radius_add_min: .3,               // 半径增加最小值
        radius_add_max: .5,               // 半径增加最大值
        opacity_min: 0.3,                 // 初始透明度最小值
        opacity_max: 0.5, 				 // 初始透明度最大值
        opacity_prev_min: .003,            // 透明度递减值最小值
        opacity_prev_max: .005,            // 透明度递减值最大值
        light_min: 40,                 // 颜色亮度最小值
        light_max: 70,                 // 颜色亮度最大值
    };
    var style_color = find_random(0, 360);
    var all_element = [];
    window_resize();

    function start() {
        window.requestAnimationFrame(start);
        style_color += .1;
        ctx.fillStyle = 'hsl(' + style_color + ',100%,97%)';
        ctx.fillRect(0, 0, w, h);
        if (all_element.length < all_attribute.num && Math.random() < all_attribute.start_probability) {
            all_element.push(new ready_run);
        }
        all_element.map(function (line) {
            line.to_step();
        })
    }

    function ready_run() {
        this.to_reset();
    }

    ready_run.prototype = {
        to_reset: function () {
            var t = this;
            t.x = find_random(0, w);
            t.y = find_random(0, h);
            t.radius = find_random(all_attribute.radius_min, all_attribute.radius_max);
            t.radius_change = find_random(all_attribute.radius_add_min, all_attribute.radius_add_max);
            t.opacity = find_random(all_attribute.opacity_min, all_attribute.opacity_max);
            t.opacity_change = find_random(all_attribute.opacity_prev_min, all_attribute.opacity_prev_max);
            t.light = find_random(all_attribute.light_min, all_attribute.light_max);
            t.color = 'hsl(' + style_color + ',100%,' + t.light + '%)';
        },
        to_step: function () {
            var t = this;
            t.opacity -= t.opacity_change;
            t.radius += t.radius_change;
            if (t.opacity <= 0) {
                t.to_reset();
                return false;
            }
            ctx.fillStyle = t.color;
            ctx.globalAlpha = t.opacity;
            ctx.beginPath();
            ctx.arc(t.x, t.y, t.radius, 0, 2 * pi, true);
            ctx.closePath();
            ctx.fill();
            ctx.globalAlpha = 1;
        }
    }

    function window_resize() {
        w = window.innerWidth;
        h = window.innerHeight;
        canvas.width = w;
        canvas.height = h;
    }

    $(window).resize(function () {
        window_resize();
    });

    function find_random(num_one, num_two) {
        return Math.random() * (num_two - num_one) + num_one;
    }

    (function () {
        var lastTime = 0;
        var vendors = ['webkit', 'moz'];
        for (var xx = 0; xx < vendors.length && !window.requestAnimationFrame; ++xx) {
            window.requestAnimationFrame = window[vendors[xx] + 'RequestAnimationFrame'];
            window.cancelAnimationFrame = window[vendors[xx] + 'CancelAnimationFrame'] ||
                window[vendors[xx] + 'CancelRequestAnimationFrame'];
        }

        if (!window.requestAnimationFrame) {
            window.requestAnimationFrame = function (callback, element) {
                var currTime = new Date().getTime();
                var timeToCall = Math.max(0, 16.7 - (currTime - lastTime));
                var id = window.setTimeout(function () {
                    callback(currTime + timeToCall);
                }, timeToCall);
                lastTime = currTime + timeToCall;
                return id;
            };
        }
        if (!window.cancelAnimationFrame) {
            window.cancelAnimationFrame = function (id) {
                clearTimeout(id);
            };
        }
    }());
    start();

}


